#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = (1<<17)+10;
ll V[maxn], D[maxn];

string in; int cnt;
void build(){
    stack<char> s;
    int d = 0;
    if(in[0]!='['){
        stringstream ss(in);
        ss >> V[cnt];
        D[cnt] = d;
        ++cnt;
    }
    for (int i = 0; i < in.size(); ++i) {
        if(in[i]=='['){
            s.push(in[i]); ++d;
        }
        else if(in[i]==']'){
            string str;
            while(s.top()!='['){
                str.insert(str.begin(), s.top());
                s.pop();
            }
            s.pop();
            int p = str.find(',');
            string left = str.substr(0, p), right = str.substr(p+1);
            int a, b;
            if(!left.empty()) {stringstream ss(left); ss>>a; V[cnt] = a; D[cnt] = d; ++cnt;}
            if(!right.empty()) {stringstream ss(right); ss>>b; V[cnt] = b; D[cnt] = d; ++cnt;}
            --d;
        }
        else s.push(in[i]);
    }
}

void init(){
    memset(V, 0, sizeof(V));
    memset(D, 0, sizeof(D));
    cnt = 0;
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
//    freopen("data.in", "r", stdin);
//    freopen("data.out", "w", stdout);
    int T; cin >> T;
    while(T--){
        init();
        cin >> in;
        build();
//        for (int i = 0; i < cnt; ++i) {
//            cout << V[i] << " " << D[i] << "\n";
//        }
//        cout << "\n";
        //system("PAUSE");
        map<ll, ll> m;
        ll keep = 0;
        for (int i = 0; i < cnt; ++i) {
            ll t = V[i] << D[i];
            if(!m.count(t)) m[t] = 1;
            else ++m[t];
            if(m[t] > keep) keep = m[t];
        }

//        for (auto &i:m) cout << i.first << " " << i.second << "\n";
//        cout << "cnt = " << cnt << ", keep = " << keep;
        cout << cnt - keep << "\n";
    }
    return 0;
}